﻿using System;
using System.DirectoryServices;
using System.Data;

using Librerias;
using Telerik.Web.UI;

namespace Aplicacion.admin.usuarios
{
    ///<summary>
    ///</summary>
    public partial class Buscar : System.Web.UI.Page
    {

        private Guid _guid;
        protected void Page_Load(object sender, EventArgs e)
        {
            bloque2.Visible = false;
            if (Page.IsPostBack)
            {
                var ad = new ObjetoAd();
                var src = ad.Buscar(txtBusqueda.Text);
                // Si encuentra mas de un usuario...
                if (src != null && src.Count >= 1)
                {

                    ltlError.Text = "";
                    int i = 0;
                    gridBusqueda.Visible = true;
                    bloque2.Visible = true;

                    #region Configurando Datatable
                    var dt = new DataTable();
                    dt.Columns.Add("ID", typeof(int));
                    dt.Columns.Add("GUID", typeof(string));
                    dt.Columns.Add("Usuario", typeof(string));
                    dt.Columns.Add("Nombre", typeof(string));
                    dt.Columns.Add("Email", typeof(string));
                    dt.Columns.Add("DNI", typeof(string));
                    dt.Columns.Add("Creado", typeof(string));
                    dt.Columns.Add("Acciones", typeof(string));
                    #endregion
                    #region Recorriendo Resultados
                    foreach (SearchResult sr in src)
                    {
                        ad.GetObjeto(sr.Path);
                        _guid = new Guid((byte[])ad.AdGuid);
                        string acciones = _guid.ToString();

                        dt.Rows.Add(new object[] { i, acciones, ad.SamAccountName, ad.DisplayName, ad.Mail, ad.DNI, ad.fechaCreacion, acciones });
                        i++;
                    }
                    #endregion

                    gridBusqueda.DataSource = dt;
                    gridBusqueda.DataBind();

                    //if (src.Count == 1)
                    //    gridBusqueda.FilterEnabled = false;

                }
                // Si encuentra un solo usuario...
                /*
                else if (src.Count == 1)
                {
                    DirectoryEntry de = AD.obtenerUsuario(src[0].Path);
                    _Guid = new AdGuid((byte[])de.Properties["objectGUID"].Value);
                    Response.Redirect(String.Format("editar.aspx?uid={0}", _Guid));
                }
                 */
                // Si no encuentra nada...
                else
                {
                    Notificaciones.Error("No se encontro ningun usuario");
                }
            }
        }

        protected void gridBusqueda_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {

        }
        /*
        protected void GridDetalleBeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["guid"] = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();

        }
         * */
        /*
        protected void GridBusquedaDetailRowExpandedChanged(object sender, ASPxGridViewDetailRowEventArgs e)
        {
            if (e.Expanded)
            {
                var gridDetalle = gridBusqueda.FindDetailRowTemplateControl(e.VisibleIndex, "gridDetalle") as ASPxGridView;
                //string guid = Session["guid"].ToString();

                var guid = new Guid(Session["guid"].ToString());


                var de = new ObjetoAd();


                de.GetObjeto(guid);
                using (var dtDetalle = new DataTable())
                {
                    dtDetalle.Columns.Add("Atributo", typeof(string));
                    dtDetalle.Columns.Add("Valor", typeof(string));
                    dtDetalle.Rows.Add(new object[] { "Usuario", de.SamAccountName });
                    dtDetalle.Rows.Add(new object[] { "Nombre", de.Nombre });
                    dtDetalle.Rows.Add(new object[] { "Apellido", de.Apellido });
                    dtDetalle.Rows.Add(new object[] { "E-Mail", de.Mail });
                    dtDetalle.Rows.Add(new object[] { "Accede a Linux", de.NisDomain != null ? "Si" : "No" });
                    if (de.NisDomain != null)
                    {
                        dtDetalle.Rows.Add(new object[] { "Dominio Nis", de.NisDomain });
                        dtDetalle.Rows.Add(new object[] { "Grupo Primario", de.GidNumber });
                        dtDetalle.Rows.Add(new object[] { "UID", de.UidNumber });
                        dtDetalle.Rows.Add(new object[] { "Shell Login", de.LoginShell });
                        dtDetalle.Rows.Add(new object[] { "Directorio Home", de.HomeDirectory });
                    }
                    if (gridDetalle != null)
                    {
                        gridDetalle.DataSource = dtDetalle;
                        gridDetalle.DataBind();
                    }
                }




            }
        }*/


    }
}